home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / persist.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-14  |  6.0 KB  |  162 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- //
  5. // C++ Header File Name: persist.h 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer    
  8. // File Creation Date: 09/18/1997  
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ---------- Include File Description and Details  ---------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. The (P)ersistent base class is used to define the interface 
  32. that makes the object persistent. 
  33.  
  34. Changes: 
  35. ================================================================
  36. 01/20/1998 - Added overloaded versions of the StringFileLength(),
  37. WriteString(), and ReadString() functions used to operate on
  38. low level c strings.
  39. Added by: Doug Gaer
  40.  
  41. 02/04/1998: Added pure virtual functions Delete() and Remove.
  42. These functions must be defined in the derived class.
  43. Added by: Doug Gaer
  44.  
  45. 02/04/1998: Modifed DeleteObject and RemoveObject functions to
  46. work with the Delete() and Remove() functions defined in the
  47. derived class.
  48. Changed by: Doug Gaer
  49.  
  50. 02/04/1998: Added new functions to work with index files.
  51. Added by: Doug Gaer
  52.  
  53. 09/21/1998: Changed null_byte in all versions of WriteString()
  54. to 0 instead of '\0' because the null character is not a valid
  55. binary number.
  56. Changed by: Doug Gaer
  57.  
  58. 10/02/1998: Modified WriteObjHeader(), ReadObjHeader(),
  59. WriteString(), and ReadString() functions to use the
  60. current file address as a default argument.
  61. Changed by: Doug Gaer
  62.  
  63. 10/09/1998: Modified the AddKey() and RemoveKey() functions to
  64. flush the Btree cache after each insertion or deletion by
  65. default.
  66. Changed by: Doug Gaer
  67. ================================================================
  68. */
  69. // ----------------------------------------------------------- //   
  70. #ifndef __PERSIST_HPP
  71. #define __PERSIST_HPP
  72.  
  73. #include "pod.h"
  74.  
  75. // (O)bject (H)eader (8 bytes total)
  76. struct ObjectHeader
  77. {
  78.   INT32 ClassID;    // Class Identification number (4 bytes)
  79.   FAU ObjectID;     // Object Identification number (4 bytes)
  80. };
  81.  
  82. // (P)ersistent base class
  83. class Persistent
  84. {
  85. public:
  86.   Persistent(POD *DB) { Connect(DB); objectaddress = 0; }
  87.   Persistent(const POD *DB) { Connect(DB); objectaddress = 0; }
  88.   Persistent() { pod = 0; } 
  89.  
  90. private:
  91.   Persistent(const Persistent &ob) { } // Disallow coping
  92.   void operator=(const Persistent &ob) { } // Disallow assignment
  93.   
  94. protected: // Derived class interface
  95.   virtual INT32 GetClassID() const = 0;
  96.   virtual const char *GetClassName() = 0;
  97.   virtual FAU Write() = 0;
  98.   virtual void Read(FAU Address) = 0;
  99.   virtual FAU Find() = 0;
  100.   virtual __UWORD__ ObjectLength() = 0;
  101.   virtual FAU GetObjectAddress() = 0;
  102.   virtual void SetObjectAddress(FAU addr) = 0;
  103.  
  104.   // 02/04/1998: Added to work with index files
  105.   virtual FAU Delete() = 0; // Mark object deleted keeping data intact
  106.   virtual FAU Remove() = 0; // Permanently remove object from data file 
  107.   
  108. public:
  109.   // Override these functions only if an index file is used
  110.   virtual int CompareIndex();
  111.   virtual int RebuildIndexFile(const char *fname);
  112.   
  113. protected:
  114.   void WriteObjHeader(const ObjectHeader &oh, FAU addr = CurrAddress);
  115.   void ReadObjHeader(ObjectHeader &oh, FAU addr = CurrAddress);
  116.   void Connect(POD *DB); // Connects persistent class to open database
  117.   void Connect(const POD *DB);
  118.   __UWORD__ StringFileLength(const UString &s); // Length of string in file
  119.   __UWORD__ StringFileLength(const char *s);
  120.   __UWORD__ StringFileLength(char *s); 
  121.   void WriteString(const UString &s, FAU addr = CurrAddress);
  122.   void WriteString(const char *s, FAU addr = CurrAddress);
  123.   void WriteString(char *s, FAU addr = CurrAddress);
  124.   void ReadString(UString &s, FAU addr = CurrAddress);
  125.   char *ReadString(FAU addr = CurrAddress);
  126.  
  127. protected: // Index file functions
  128.   int AddKey(EntryKey &key, int flush = 1);
  129.   int Persistent::FindKey(EntryKey &key, int full_search = 0);
  130.   int Persistent::RemoveKey(EntryKey &key, int flush = 1);
  131.   
  132. public: // User interface
  133.   FAU AddObject(int find = 1);
  134.   void ReadObject(FAU addr) { Read(addr); }
  135.   FAU FindObject() { return Find(); }
  136.   FAU ObjectAddress(int find = 1);
  137.   void DeleteObject(FAU addr);
  138.   void RemoveObject(FAU addr);
  139.   
  140.   // 02/04/1998: These functions have been modified to
  141.   // work with index files. The Delete() and Remove()
  142.   // functions must be defined in the derived class.
  143.   FAU DeleteObject() { return Delete(); }
  144.   FAU RemoveObject() { return Remove(); }
  145.  
  146. public: // Index file functions
  147.   int UsingIndex() { return pod->UsingIndex(); }
  148.   int UsingIndex() const { return pod->UsingIndex(); }
  149.   int RebuildIndex() { return pod->RebuildIndex(); }
  150.   int RebuildIndex() const { return pod->RebuildIndex(); }
  151.   
  152. protected:
  153.   POD *pod;
  154.   FAU objectaddress;
  155. };
  156.  
  157. #endif // __PERSIST_HPP
  158. // ----------------------------------------------------------- // 
  159. // ------------------------------- //
  160. // --------- End of File --------- //
  161. // ------------------------------- //
  162.